home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / libs / mufs_usergroup.lha / usergroup / console.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  4KB  |  171 lines

  1. RCS_ID_C="$Id: console.c,v 1.3 1994/01/21 08:31:06 ppessi Exp $";
  2. /*
  3.  * Console handling
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP User Library.
  8.  *
  9.  * Copyright ⌐ 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *
  12.  * Created      : Sun Nov 28 17:45:55 1993 ppessi
  13.  * Last modified: Fri Jan 21 07:27:44 1994 ppessi
  14.  *
  15.  * $Log: console.c,v $
  16.  * Revision 1.3  1994/01/21  08:31:06  ppessi
  17.  * Added "libfunc.h" include
  18.  *
  19.  * Revision 1.2  1994/01/21  08:13:01  ppessi
  20.  * Updated documentation
  21.  *
  22.  * Revision 1.1  1994/01/19  10:03:09  ppessi
  23.  * Initial revision
  24.  *
  25.  * Revision 1.1  93/11/30  03:09:47  ppessi
  26.  * Initial revision
  27.  *
  28.  */
  29.  
  30. #include "base.h"
  31. #include "libfunc.h"
  32. #include <string.h>
  33.  
  34. /****** usergroup.library/ug_OnConsole *************************************
  35.  
  36.     NAME
  37.         ug_OnConsole - check whether session is on local console
  38.  
  39.     SYNOPSIS
  40.         result = ug_OnConsole(void)
  41.         D0
  42.  
  43.         BOOL ug_OnConsole(void)
  44.  
  45.     FUNCTION
  46.         Check if the user is logged on local console.
  47.  
  48.     RESULT
  49.         result - 1 if the user is on console,
  50.                  0 otherwise.
  51.  
  52.     BUGS
  53.  
  54.         Currently checking is done depending on the process window pointer.
  55.  
  56.     SEE ALSO
  57.  
  58. ****************************************************************************
  59. */
  60.  
  61. SAVEDS ASM int R_ug_OnConsole(void)
  62. {
  63.   struct Process *me = (struct Process *)FindTask(NULL);
  64.  
  65.   return
  66.     me->pr_Task.tc_Node.ln_Type == NT_PROCESS &&
  67.     me->pr_WindowPtr != (APTR)-1;
  68. }
  69.  
  70. /****** usergroup.library/ug_GetConsoleName **********************************
  71.  
  72.     NAME
  73.         ug_GetConsoleName --- Get Console Identifier
  74.  
  75.     SYNOPSIS
  76.         name = ug_GetConsoleName(fh, buffer, size)
  77.         D0                    D0  A0      D1
  78.  
  79.         UBYTE * ug_GetConsoleName(BPTR, UBYTE *, ULONG)
  80.  
  81.     FUNCTION
  82.         Get a unique printable identifier for the interactive filehandle.
  83.         This identifier is usually the task name of handler concatenated
  84.         with message port address.
  85.  
  86.     INPUTS
  87.         fh     - An interactive filehandle
  88.         buffer - Buffer to hold console identifier
  89.         size   - Number of bytes in buffer.
  90.  
  91.     RESULT
  92.         name - If call is successful, pointer to buffer. NULL if
  93.                error.
  94.  
  95.     BUGS
  96.         May not get the proprer console name for all different console
  97.         handlers.
  98.  
  99.     SEE ALSO
  100.         dos.library/GetConsoleTask()
  101.  
  102. *****************************************************************************
  103. */
  104.  
  105. char *i_GetConsoleName(struct MsgPort *consoletask, char *buffer, ULONG size)
  106. {
  107.   struct Node *portowner = consoletask->mp_SigTask;
  108.   static char hex_chars[] = "0123456789abcdef";
  109.   ULONG ctx = (ULONG) consoletask;
  110.   STRPTR poname;
  111.   ULONG i;
  112.  
  113.   /* Fail if the there is no task for this port */
  114.   if (portowner == NULL ||
  115.       (consoletask->mp_Node.ln_Type != PA_SIGNAL &&
  116.        consoletask->mp_Node.ln_Type != PA_SOFTINT) ||
  117.       portowner->ln_Name == NULL) {
  118.     poname = "XXX";
  119.   } else {
  120.     /* OK, port owner has got a name, use it */
  121.     poname = portowner->ln_Name;
  122.   }
  123.  
  124.   i = strlen(poname);
  125.   if (i > 6)
  126.     i = 6;
  127.   if (i > size)
  128.     i = size;
  129.   memcpy(buffer, poname, size);
  130.  
  131.   /* Append with ":%06x" consoletask */
  132.   if (i < size) {
  133.     short n;
  134.  
  135.     for (n = 8, buffer[i++] = ':'; n > 0 && i < size; n--) {
  136.       int hex_digit = ctx >> 28 & 0xf;
  137.       if (n < 7 || hex_digit > 0)
  138.         buffer[i++] = hex_chars[hex_digit];
  139.       ctx <<= 4;
  140.     }
  141.   }
  142.  
  143.   /* NUL terminate */
  144.   if (i < size) {
  145.     buffer[i] = '\0';
  146.     return buffer;
  147.   } else {
  148.     return NULL;
  149.   }
  150. }
  151.  
  152. SAVEDS ASM
  153. char *R_ug_ConsoleName(REG(d0) LONG con, REG(a1) char *buffer,
  154.                        REG(d1) ULONG size)
  155. {
  156.   struct FileHandle *confh = BADDR(con);
  157.  
  158.   if (con == 0)
  159.     return NULL;
  160.  
  161.   if (!IsInteractive(con))
  162.     return NULL;
  163.  
  164.   if (buffer == NULL || size < 1)
  165.     return NULL;
  166.  
  167.   return i_GetConsoleName(confh->fh_Type, buffer, size);
  168. }
  169.  
  170.  
  171.